package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.OfflineMapActivity;
import com.hongxun.design.bean.CitysBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.progressstyle.MyProgressBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/14.
 */
public class CitysextendsBaseExpandableListAdapter extends BaseExpandableListAdapter {
    Context context;
    List<CitysBean> itemBeans;
    List<List<CitysBean>> childBeans;
    LayoutInflater inflater;

    public CitysextendsBaseExpandableListAdapter(Context context, List<CitysBean> itemBeans) {
        this.context = context;
        this.itemBeans = checkData(itemBeans);
        inflater = LayoutInflater.from(context);
    }


    public List<CitysBean> checkData(List<CitysBean> itemBeans) {
        if (childBeans == null) childBeans = new ArrayList<>();
        childBeans.clear();
        for (CitysBean c : itemBeans) {
            if (c.getChilds() != null && c.getChilds().size() > 0) {
                childBeans.add(c.getChilds());
            } else {
                childBeans.add(new ArrayList<CitysBean>());
            }
        }


        return itemBeans;
    }

    @Override
    public int getGroupCount() {
        return itemBeans.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childBeans.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return itemBeans.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childBeans.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        CitysBean bean = (CitysBean) getGroup(i);
        Holldler holldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_offlinemapcity, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }
        try {
            if (bean.isDisplay()) {
                holldler.type.setVisibility(View.VISIBLE);
            } else {
                holldler.type.setVisibility(View.GONE);
            }
            holldler.progressBar.setVisibility(View.GONE);
            holldler.type.setText(bean.getCityType());
            holldler.name.setText(bean.getCityName());
            holldler.size.setText(bean.getCitySize());
            if (bean.getChilds().size() > 0) {
                bean.setCityCode(OfflineMapActivity.Code_child);
            }
            holldler.code.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    CitysBean bean = (CitysBean) msg;
                    switch (bean.getCityCode()) {
                        case OfflineMapActivity.Code_notload:
                            startLoad(bean);//下载
                            break;
                        case OfflineMapActivity.Code_isloading:
                            stopLoad(bean);//暂停
                            break;
                        case OfflineMapActivity.Code_continue:
                            continueLoad(bean);//继续
                            break;
                        case OfflineMapActivity.Code_update:
                            update(bean);
                            break;

                        case OfflineMapActivity.Code_child:
                            if(((OfflineMapActivity)context).citysFragment.citys_list.isGroupExpanded(position)){
                                ((OfflineMapActivity)context).citysFragment.citys_list.collapseGroup(position);
                            }else{
                                //第二个参数false表示展开时是否触发默认滚动动画
                                ((OfflineMapActivity)context).citysFragment.citys_list.expandGroup(position,false);
                            }
                            break;
                    }
                    notifyDataSetChanged();
                }
            });


            switch (bean.getCityCode()) {

                case OfflineMapActivity.Code_notload:
                    holldler.code.setText("下载");
                    setTextSize(holldler.code, 1);
                    break;
                case OfflineMapActivity.Code_load:
                    holldler.code.setText("已下载");
                    setTextSize(holldler.code, 0);
                    break;
                case OfflineMapActivity.Code_update:
                    holldler.code.setText("更新");
                    setTextSize(holldler.code, 1);
                    break;
                case OfflineMapActivity.Code_isloading:
                    holldler.code.setText("暂停");
                    setTextSize(holldler.code, 1);
                    holldler.progressBar.setVisibility(View.VISIBLE);
                    holldler.progressBar.setProgress(bean.getValueCode());
                    break;
                case OfflineMapActivity.Code_continue:
                    holldler.code.setText("继续");
                    setTextSize(holldler.code, 1);
                    holldler.progressBar.setVisibility(View.VISIBLE);
                    holldler.progressBar.setProgress(bean.getValueCode());
                    break;
                case OfflineMapActivity.Code_child:
                    SetTypeface.set(holldler.code, IconSourse.DROPDOWN);
                    setTextSize(holldler.code, 0);
                     break;
            }


        } catch (Exception e) {
        }


        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        CitysBean bean = (CitysBean) getChild(i, i1);
        CHolldler holldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_offlinemapcitychild, null);
            holldler = new CHolldler(view);
            view.setTag(holldler);
        } else {
            holldler = (CHolldler) view.getTag();
        }
        try {
            holldler.progressBar.setVisibility(View.GONE);
            holldler.name.setText(bean.getCityName());
            holldler.size.setText(bean.getCitySize());
            switch (bean.getCityCode()) {
                case OfflineMapActivity.Code_notload:
                    holldler.code.setText("下载");
                    setTextSize(holldler.code, 1);
                    break;
                case OfflineMapActivity.Code_load:
                    holldler.code.setText("已下载");
                    setTextSize(holldler.code, 0);
                    break;
                case OfflineMapActivity.Code_update:
                    holldler.code.setText("更新");
                    setTextSize(holldler.code, 1);
                    break;
                case OfflineMapActivity.Code_isloading:
                    holldler.code.setText("暂停");
                    setTextSize(holldler.code, 1);
                    holldler.progressBar.setVisibility(View.VISIBLE);
                    holldler.progressBar.setProgress(bean.getValueCode());
                    break;
                case OfflineMapActivity.Code_continue:
                    holldler.code.setText("继续");
                    setTextSize(holldler.code, 1);
                    holldler.progressBar.setVisibility(View.VISIBLE);
                    holldler.progressBar.setProgress(bean.getValueCode());
                    break;
                case OfflineMapActivity.Code_child:
                    SetTypeface.set(holldler.code, IconSourse.DROPDOWN);
                    setTextSize(holldler.code, 0);
                    break;
            }
            holldler.code.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    CitysBean bean = (CitysBean) msg;
                    switch (bean.getCityCode()) {
                        case OfflineMapActivity.Code_notload:
                            startLoad(bean);//下载
                            break;
                        case OfflineMapActivity.Code_isloading:
                            stopLoad(bean);//暂停
                            break;
                        case OfflineMapActivity.Code_continue:
                            continueLoad(bean);//继续
                            break;
                        case OfflineMapActivity.Code_update:
                            update(bean);
                            break;
                    }
                    notifyDataSetChanged();
                }
            });


        } catch (Exception e) {
        }


        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


    class Holldler {
        TextView type, name, size, code;
        MyProgressBar progressBar;

        Holldler(View v) {
            type = (TextView) v.findViewById(R.id.city_type);
            name = (TextView) v.findViewById(R.id.city_name);
            size = (TextView) v.findViewById(R.id.city_size);
            code = (TextView) v.findViewById(R.id.city_code);
            progressBar = (MyProgressBar) v.findViewById(R.id.city_code_progressBar);
            progressBar.setVisibility(View.GONE);
        }
    }

    class CHolldler {
        TextView name, size, code;
        MyProgressBar progressBar;

        CHolldler(View v) {

            name = (TextView) v.findViewById(R.id.city_name);
            size = (TextView) v.findViewById(R.id.city_size);
            code = (TextView) v.findViewById(R.id.city_code);
            progressBar = (MyProgressBar) v.findViewById(R.id.city_code_progressBar);
            progressBar.setVisibility(View.GONE);
        }
    }


    /**
     * 改变textview的字体与边框  type：0为无边框，1为有*
     */
    private void setTextSize(TextView v, int type) {
        if (type == 0) {
            v.setTextSize(14);
            v.setBackground(null);
            v.setTextColor(context.getResources().getColor(R.color.gray));
        } else {
            v.setTextSize(16);
            v.setBackgroundResource(R.drawable.blueline3_wihte_item);
            v.setTextColor(context.getResources().getColor(R.color.project_blue));
        }

    }


    /**
     * 开始下载*
     */
    private void startLoad(CitysBean bean) {
        //bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

    /**
     * 继续下载*
     */
    private void continueLoad(CitysBean bean) {
        //bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

    /**
     * 暂停下载*
     */
    private void stopLoad(CitysBean bean) {
        // bean.setCityCode(OfflineMapActivity.Code_continue);
        ((OfflineMapActivity) context).stop(bean.getCityId(), bean.getCityName());
    }


    /**
     * 开始更新*
     */
    private void update(CitysBean bean) {
        // bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

}
